package org.xwb.atomic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.LongAdder;

public class LongAdderDemo {
    public static void main(String[] args) {
        CountDownLatch downLatch = new CountDownLatch(50);
        LongAdder longAdder = new LongAdder();
        for (int i = 0; i < 50; i++) {
            new Thread(()->{
                for (int j = 0; j < 1000000; j++) {
                    longAdder.increment();
                }
                downLatch.countDown();
            },"线程"+i).start();
        }

        try {
            downLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("longAdder.sum() = " + longAdder.sum());
    }
}
